{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "# 自定义工具包\n",
    "from mail import Mail\n",
    "# 参考课程：90.综合案例-01/01.Excel工资单生成_02\n",
    "# create_salary_attachment: 创建工资明细单附件\n",
    "# get_salary_data: 获取指定文件的工资单数据\n",
    "from xlwt_ext import create_salary_attachment, get_salary_data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1、获取薪资明细数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 通过 get_salary_data 方法，获取指定文件的工资单数据\n",
    "# 员工姓名列表、字段名称列表、薪资数据列表、电子邮箱列表\n",
    "staff_names, field_names, staff_values_list, emails = get_salary_data(\"./files/Excel_工资单明细_电子邮件.xls\")\n",
    "print(\"【员工姓名】：\",staff_names)\n",
    "print(\"【字段名称】：\",field_names)\n",
    "print(\"【薪资数据】：\",staff_values_list)\n",
    "print(\"【电子邮箱】：\",emails)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2、创建一个邮件对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建一个邮件对象\n",
    "mail = Mail.build_mail(mail_addr=\"xxxxx@126.com\",\n",
    "                       password=\"IPBPMGYMCKDULBMM\",\n",
    "                       smtp_server=\"smtp.126.com\",\n",
    "                       display_name=\"超人科技人事部\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3、遍历每个员工、生成附件、发送邮件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 遍历每个员工的薪资数据，生成工资明细单附件，给指定的电子邮箱发送邮件\n",
    "for staff_index, staff_name in enumerate(staff_names):\n",
    "    # 获取该员工的薪资数据\n",
    "    staff_data = staff_values_list[staff_index]\n",
    "    # 获取该员工的电子邮箱\n",
    "    email = emails[staff_index]\n",
    "    # 创建该员工的工资明细单附件\n",
    "    attachment = create_salary_attachment(staff_name, field_names,staff_data)\n",
    "    # 工资明细单附件的绝对路径\n",
    "    attachment_path = os.path.abspath(attachment)\n",
    "    \n",
    "    # 邮件的标题\n",
    "    mail_subject = \"2022年10月工资明细\"\n",
    "    # 邮件的内容\n",
    "    mail_content = staff_name + \"您好！\\n工资已到账，请看附件。\"\n",
    "    # 注意：发送前把所有员工的 email 改为一个测试账号，确保没有问题后，注释即可\n",
    "    email = \"\"\n",
    "    \n",
    "    mail.send_text(email, mail_subject, mail_content, attachment_paths=[attachment_path])\n",
    "    \n",
    "    # 发送成功后，删除磁盘上的文件，释放空间\n",
    "    os.remove(attachment_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
